package vnt.farming;

import java.util.ArrayList;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.textservice.SuggestionsInfo;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Filter;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ListThuocAdapter extends ArrayAdapter<TblThuoc>{
	private ArrayList<TblThuoc> thuocs;
	public ArrayList<TblThuoc> getThuocs() { return thuocs; }
	public void setThuocs(ArrayList<TblThuoc> pThuocs){ thuocs = pThuocs; }
    int resourceId;
    Context context;
    
    public ListThuocAdapter(Context context, int textViewResourceId,
    		ArrayList<TblThuoc> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        resourceId = textViewResourceId;
        thuocs = objects;        
    }    

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
        	LayoutInflater inflater = ((MainActivity)context).getLayoutInflater();
        	convertView = inflater.inflate(resourceId, parent, false);
        }
        
        final TblThuoc thuoc = thuocs.get(position);
        //((MainActivity)context).tblThuoc = thuoc;
        
        if (thuoc != null) {
            TextView tenThuoc = (TextView) convertView.findViewById(R.id.tenThuoc);
            
            tenThuoc.setText(thuoc.getTenThuoc());
        }
        
        return convertView;
    }    
    
    @Override
    public Filter getFilter() {
    	// TODO Auto-generated method stub
    	return nameFilter;
    }
    
    Filter nameFilter = new Filter(){
    	public String convertResultToString(Object resultValue){
    		String value = ((TblThuoc)(resultValue)).getTenThuoc();
    		return value;
    	}
    	
    	@Override
    	protected FilterResults performFiltering(CharSequence constraint) {
    		if(constraint != null)
    		{
    			FilterResults filterResults = new FilterResults();
    			filterResults.values = thuocs;
    			filterResults.count = thuocs.size();
    			return filterResults;
    		}
    		else
    		{
    			return new FilterResults();
    		}
    	}

		@Override
		protected void publishResults(CharSequence constraint,
				FilterResults results) {
			// TODO Auto-generated method stub
			if(results != null && results.count > 0){
				notifyDataSetChanged();
			}
			else{
				notifyDataSetInvalidated();
			}

			/*
			try{
			if(results != null && results.count > 0){
				clear();
				ArrayList<TblThuoc> items = (ArrayList<TblThuoc>)results.values;
				for(TblThuoc item : items){
					try{
						add(item);
					}
					catch(Exception ex){
						Log.e("publishResults.add(item)", ex.getMessage());
					}
				}
				notifyDataSetChanged();
			}
			else{
				notifyDataSetInvalidated();
			}
			}catch(Exception ex1){
				Log.e("publishResults", ex1.getMessage());
			}
			*/
		};
    };
}